<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='Ext-data-XmlWriter'>/**
</span> * @class Ext.data.XmlWriter
 * @extends Ext.data.DataWriter
 * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action via XML.
 * XmlWriter uses an instance of {@link Ext.XTemplate} for maximum flexibility in defining your own custom XML schema if the default schema is not appropriate for your needs.
 * See the {@link #tpl} configuration-property.
 */
Ext.data.XmlWriter = function(params) {
    Ext.data.XmlWriter.superclass.constructor.apply(this, arguments);
    // compile the XTemplate for rendering XML documents.
    this.tpl = (typeof(this.tpl) === 'string') ? new Ext.XTemplate(this.tpl).compile() : this.tpl.compile();
};
Ext.extend(Ext.data.XmlWriter, Ext.data.DataWriter, {
<span id='Ext-data-XmlWriter-cfg-documentRoot'>    /**
</span>     * @cfg {String} documentRoot [xrequest] (Optional) The name of the XML document root-node.  &lt;b&gt;Note:&lt;/b&gt;
     * this parameter is required &lt;/b&gt;only when&lt;/b&gt; sending extra {@link Ext.data.Store#baseParams baseParams} to the server
     * during a write-request -- if no baseParams are set, the {@link Ext.data.XmlReader#record} meta-property can
     * suffice as the XML document root-node for write-actions involving just a &lt;b&gt;single record&lt;/b&gt;.  For requests
     * involving &lt;b&gt;multiple&lt;/b&gt; records and &lt;b&gt;NO&lt;/b&gt; baseParams, the {@link Ext.data.XmlWriter#root} property can
     * act as the XML document root.
     */
    documentRoot: 'xrequest',
<span id='Ext-data-XmlWriter-cfg-forceDocumentRoot'>    /**
</span>     * @cfg {Boolean} forceDocumentRoot [false] Set to &lt;tt&gt;true&lt;/tt&gt; to force XML documents having a root-node as defined
     * by {@link #documentRoot}, even with no baseParams defined.
     */
    forceDocumentRoot: false,
<span id='Ext-data-XmlWriter-cfg-root'>    /**
</span>     * @cfg {String} root [records] The name of the containing element which will contain the nodes of an write-action involving &lt;b&gt;multiple&lt;/b&gt; records.  Each
     * xml-record written to the server will be wrapped in an element named after {@link Ext.data.XmlReader#record} property.
     * eg:
&lt;code&gt;&lt;pre&gt;
&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&amp;lt;user&gt;&amp;lt;first&gt;Barney&amp;lt;/first&gt;&amp;lt;/user&gt;
&lt;/code&gt;&lt;/pre&gt;
     * However, when &lt;b&gt;multiple&lt;/b&gt; records are written in a batch-operation, these records must be wrapped in a containing
     * Element.
     * eg:
&lt;code&gt;&lt;pre&gt;
&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &amp;lt;records&gt;
        &amp;lt;first&gt;Barney&amp;lt;/first&gt;&amp;lt;/user&gt;
        &amp;lt;records&gt;&amp;lt;first&gt;Barney&amp;lt;/first&gt;&amp;lt;/user&gt;
    &amp;lt;/records&gt;
&lt;/code&gt;&lt;/pre&gt;
     * Defaults to &lt;tt&gt;records&lt;/tt&gt;.  Do not confuse the nature of this property with that of {@link #documentRoot}
     */
    root: 'records',
<span id='Ext-data-XmlWriter-cfg-xmlVersion'>    /**
</span>     * @cfg {String} xmlVersion [1.0] The &lt;tt&gt;version&lt;/tt&gt; written to header of xml documents.
&lt;code&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-15&quot;?&gt;&lt;/pre&gt;&lt;/code&gt;
     */
    xmlVersion : '1.0',
<span id='Ext-data-XmlWriter-cfg-xmlEncoding'>    /**
</span>     * @cfg {String} xmlEncoding [ISO-8859-15] The &lt;tt&gt;encoding&lt;/tt&gt; written to header of xml documents.
&lt;code&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-15&quot;?&gt;&lt;/pre&gt;&lt;/code&gt;
     */
    xmlEncoding: 'ISO-8859-15',
<span id='Ext-data-XmlWriter-cfg-tpl'>    /**
</span>     * @cfg {String/Ext.XTemplate} tpl The XML template used to render {@link Ext.data.Api#actions write-actions} to your server.
     * &lt;p&gt;One can easily provide his/her own custom {@link Ext.XTemplate#constructor template-definition} if the default does not suffice.&lt;/p&gt;
     * &lt;p&gt;Defaults to:&lt;/p&gt;
&lt;code&gt;&lt;pre&gt;
&amp;lt;?xml version=&quot;{version}&quot; encoding=&quot;{encoding}&quot;?&gt;
    &amp;lt;tpl if=&quot;documentRoot&quot;&gt;&amp;lt;{documentRoot}&gt;
    &amp;lt;tpl for=&quot;baseParams&quot;&gt;
        &amp;lt;tpl for=&quot;.&quot;&gt;
            &amp;lt;{name}&gt;{value}&amp;lt;/{name}&gt;
        &amp;lt;/tpl&gt;
    &amp;lt;/tpl&gt;
    &amp;lt;tpl if=&quot;records.length &amp;gt; 1&quot;&gt;&amp;lt;{root}&gt;',
    &amp;lt;tpl for=&quot;records&quot;&gt;
        &amp;lt;{parent.record}&gt;
        &amp;lt;tpl for=&quot;.&quot;&gt;
            &amp;lt;{name}&gt;{value}&amp;lt;/{name}&gt;
        &amp;lt;/tpl&gt;
        &amp;lt;/{parent.record}&gt;
    &amp;lt;/tpl&gt;
    &amp;lt;tpl if=&quot;records.length &amp;gt; 1&quot;&gt;&amp;lt;/{root}&gt;&amp;lt;/tpl&gt;
    &amp;lt;tpl if=&quot;documentRoot&quot;&gt;&amp;lt;/{documentRoot}&gt;&amp;lt;/tpl&gt;
&lt;/pre&gt;&lt;/code&gt;
     * &lt;p&gt;Templates will be called with the following API&lt;/p&gt;
     * &lt;ul&gt;
     * &lt;li&gt;{String} version [1.0] The xml version.&lt;/li&gt;
     * &lt;li&gt;{String} encoding [ISO-8859-15] The xml encoding.&lt;/li&gt;
     * &lt;li&gt;{String/false} documentRoot The XML document root-node name or &lt;tt&gt;false&lt;/tt&gt; if not required.  See {@link #documentRoot} and {@link #forceDocumentRoot}.&lt;/li&gt;
     * &lt;li&gt;{String} record The meta-data parameter defined on your {@link Ext.data.XmlReader#record} configuration represents the name of the xml-tag containing each record.&lt;/li&gt;
     * &lt;li&gt;{String} root The meta-data parameter defined by {@link Ext.data.XmlWriter#root} configuration-parameter.  Represents the name of the xml root-tag when sending &lt;b&gt;multiple&lt;/b&gt; records to the server.&lt;/li&gt;
     * &lt;li&gt;{Array} records The records being sent to the server, ie: the subject of the write-action being performed.  The records parameter will be always be an array, even when only a single record is being acted upon.
     *     Each item within the records array will contain an array of field objects having the following properties:
     *     &lt;ul&gt;
     *         &lt;li&gt;{String} name The field-name of the record as defined by your {@link Ext.data.Record#create Ext.data.Record definition}.  The &quot;mapping&quot; property will be used, otherwise it will match the &quot;name&quot; property.  Use this parameter to define the XML tag-name of the property.&lt;/li&gt;
     *         &lt;li&gt;{Mixed} value The record value of the field enclosed within XML tags specified by name property above.&lt;/li&gt;
     *     &lt;/ul&gt;&lt;/li&gt;
     * &lt;li&gt;{Array} baseParams.  The baseParams as defined upon {@link Ext.data.Store#baseParams}.  Note that the baseParams have been converted into an array of [{name : &quot;foo&quot;, value: &quot;bar&quot;}, ...] pairs in the same manner as the &lt;b&gt;records&lt;/b&gt; parameter above.  See {@link #documentRoot} and {@link #forceDocumentRoot}.&lt;/li&gt;
     * &lt;/ul&gt;
     */
    // Encoding the ? here in case it's being included by some kind of page that will parse it (eg. PHP)
    tpl: '&lt;tpl for=&quot;.&quot;&gt;&lt;\u003fxml version=&quot;{version}&quot; encoding=&quot;{encoding}&quot;\u003f&gt;&lt;tpl if=&quot;documentRoot&quot;&gt;&lt;{documentRoot}&gt;&lt;tpl for=&quot;baseParams&quot;&gt;&lt;tpl for=&quot;.&quot;&gt;&lt;{name}&gt;{value}&lt;/{name}&gt;&lt;/tpl&gt;&lt;/tpl&gt;&lt;/tpl&gt;&lt;tpl if=&quot;records.length&amp;gt;1&quot;&gt;&lt;{root}&gt;&lt;/tpl&gt;&lt;tpl for=&quot;records&quot;&gt;&lt;{parent.record}&gt;&lt;tpl for=&quot;.&quot;&gt;&lt;{name}&gt;{value}&lt;/{name}&gt;&lt;/tpl&gt;&lt;/{parent.record}&gt;&lt;/tpl&gt;&lt;tpl if=&quot;records.length&amp;gt;1&quot;&gt;&lt;/{root}&gt;&lt;/tpl&gt;&lt;tpl if=&quot;documentRoot&quot;&gt;&lt;/{documentRoot}&gt;&lt;/tpl&gt;&lt;/tpl&gt;',


<span id='Ext-data-XmlWriter-method-render'>    /**
</span>     * XmlWriter implementation of the final stage of a write action.
     * @param {Object} params Transport-proxy's (eg: {@link Ext.Ajax#request}) params-object to write-to.
     * @param {Object} baseParams as defined by {@link Ext.data.Store#baseParams}.  The baseParms must be encoded by the extending class, eg: {@link Ext.data.JsonWriter}, {@link Ext.data.XmlWriter}.
     * @param {Object/Object[]} data Data-object representing the compiled Store-recordset.
     */
    render : function(params, baseParams, data) {
        baseParams = this.toArray(baseParams);
        params.xmlData = this.tpl.applyTemplate({
            version: this.xmlVersion,
            encoding: this.xmlEncoding,
            documentRoot: (baseParams.length &gt; 0 || this.forceDocumentRoot === true) ? this.documentRoot : false,
            record: this.meta.record,
            root: this.root,
            baseParams: baseParams,
            records: (Ext.isArray(data[0])) ? data : [data]
        });
    },

<span id='Ext-data-XmlWriter-method-createRecord'>    /**
</span>     * createRecord
     * @protected
     * @param {Ext.data.Record} rec
     * @return {Array} Array of &lt;tt&gt;name:value&lt;/tt&gt; pairs for attributes of the {@link Ext.data.Record}.  See {@link Ext.data.DataWriter#toHash}.
     */
    createRecord : function(rec) {
        return this.toArray(this.toHash(rec));
    },

<span id='Ext-data-XmlWriter-method-updateRecord'>    /**
</span>     * updateRecord
     * @protected
     * @param {Ext.data.Record} rec
     * @return {Array} Array of {name:value} pairs for attributes of the {@link Ext.data.Record}.  See {@link Ext.data.DataWriter#toHash}.
     */
    updateRecord : function(rec) {
        return this.toArray(this.toHash(rec));

    },
<span id='Ext-data-XmlWriter-method-destroyRecord'>    /**
</span>     * destroyRecord
     * @protected
     * @param {Ext.data.Record} rec
     * @return {Array} Array containing a attribute-object (name/value pair) representing the {@link Ext.data.DataReader#idProperty idProperty}.
     */
    destroyRecord : function(rec) {
        var data = {};
        data[this.meta.idProperty] = rec.id;
        return this.toArray(data);
    }
});
</pre>
</body>
</html>
